package com.example.perplonews

import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.example.perplonews.a_b_utils.FragManager
import com.example.perplonews.a_d_customview.Bottom_VIew
import com.example.perplonews.a_d_fragment.a_news.NewsFragment
import com.example.perplonews.a_d_fragment.b_perplo.PerploFragment
import com.example.perplonews.a_d_fragment.c_video.VideoFragment
import com.example.perplonews.a_d_fragment.d_live.LiveFragment
import com.example.perplonews.a_d_fragment.e_mine.MineFragment
import java.util.*

class MainActivity : AppCompatActivity(), Bottom_VIew.ClickBar {

    private val bottomNews: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.bottom_news) }
    private val bottomPeople: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.bottom_people) }
    private val bottomVideo: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.bottom_video) }
    private val bottomLive: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.bottom_live) }
    private val bottomMine: Bottom_VIew by lazy { findViewById<Bottom_VIew>(R.id.bottom_mine) }
    private var newsfragment = NewsFragment()
    private var perplofragment = PerploFragment()
    private var videofragment = VideoFragment()
    private var livefragment = LiveFragment()
    private var minefragment = MineFragment()

    private var fragmentmap: HashMap<FragManager.Companion.FragType, Fragment>? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initVIew()
        initBottom()

    }


    private fun initVIew() {
        fragmentmap = HashMap()

    }

    //设置底部选择栏
    private fun initBottom() {
        bottomNews.addselectimage(R.mipmap.one2)
            .addtext("新闻", -0xcccccd).show()
        bottomPeople.addselectimage(R.mipmap.two2)
            .addtext("人民号", -0xcccccd).show()
        bottomVideo.addselectimage(R.mipmap.three2)
            .addtext("视频", -0xcccccd).show()
        bottomLive.addselectimage(R.mipmap.four2)
            .addtext("直播", -0xcccccd).show()
        bottomMine.addselectimage(R.mipmap.five2)
            .addtext("我的", -0xcccccd).show()
        bottomNews.setListenner(this)
        bottomPeople.setListenner(this)
        bottomVideo.setListenner(this)
        bottomLive.setListenner(this)
        bottomMine.setListenner(this)
        showfragment(R.id.bottom_news)
    }

    override fun onItem(id: Int) {
        hindfragment()
        showfragment(id)
    }

    private fun hindfragment() {
        var sf = supportFragmentManager.beginTransaction()
        if (newsfragment != null) {
            sf.hide(newsfragment)
        }
        if (perplofragment != null) {
            sf.hide(perplofragment)
        }
        if (videofragment != null) {
            sf.hide(videofragment)
        }
        if (livefragment != null) {
            sf.hide(livefragment)
        }
        if (minefragment != null) {
            sf.hide(minefragment)
        }
        sf.commit()
    }

    private fun showfragment(id: Int) {
        var sf = supportFragmentManager.beginTransaction()
        when (id) {
            R.id.bottom_news -> {
                news()
                if (newsfragment.isAdded) {
                    sf.show(newsfragment)
                } else {
                    sf.add(R.id.main_vip, newsfragment)
                    sf.show(newsfragment)
                }
            }
            R.id.bottom_people -> {
                peopre()
                if (perplofragment.isAdded) {
                    sf.show(perplofragment)
                } else {
                    sf.add(R.id.main_vip, perplofragment)
                    sf.show(perplofragment)
                }
            }
            R.id.bottom_video -> {
                video()
                if (videofragment.isAdded) {
                    sf.show(videofragment)
                } else {
                    sf.add(R.id.main_vip, videofragment)
                    sf.show(videofragment)
                }
            }
            R.id.bottom_live -> {
                live()
                if (livefragment.isAdded) {
                    sf.show(livefragment)
                } else {
                    sf.add(R.id.main_vip, livefragment)
                    sf.show(livefragment)
                }
            }
            R.id.bottom_mine -> {
                mine()
                if (minefragment.isAdded) {
                    sf.show(minefragment)
                } else {
                    sf.add(R.id.main_vip, minefragment)
                    sf.show(minefragment)
                }
            }
        }
        sf.commit()

    }


    fun news() {
        bottomNews.selectbar(R.mipmap.one, Color.RED)
        bottomPeople.unselectbar(R.mipmap.two2, -0xcccccd)
        bottomVideo.unselectbar(R.mipmap.three2, -0xcccccd)
        bottomLive.unselectbar(R.mipmap.four2, -0xcccccd)
        bottomMine.unselectbar(R.mipmap.five2, -0xcccccd)
    }

    fun peopre() {
        bottomNews.unselectbar(R.mipmap.one2, -0xcccccd)
        bottomPeople.selectbar(R.mipmap.two, Color.RED)
        bottomVideo.unselectbar(R.mipmap.three2, -0xcccccd)
        bottomLive.unselectbar(R.mipmap.four2, -0xcccccd)
        bottomMine.unselectbar(R.mipmap.five2, -0xcccccd)
        var sf = supportFragmentManager.beginTransaction()
    }

    fun video() {
        bottomNews.unselectbar(R.mipmap.one2, -0xcccccd)
        bottomPeople.unselectbar(R.mipmap.two2, -0xcccccd)
        bottomVideo.selectbar(R.mipmap.three, Color.RED)
        bottomLive.unselectbar(R.mipmap.four2, -0xcccccd)
        bottomMine.unselectbar(R.mipmap.five2, -0xcccccd)
    }

    fun live() {
        bottomNews.unselectbar(R.mipmap.one2, -0xcccccd)
        bottomPeople.unselectbar(R.mipmap.two2, -0xcccccd)
        bottomVideo.unselectbar(R.mipmap.three2, -0xcccccd)
        bottomLive.selectbar(R.mipmap.four, Color.RED)
        bottomMine.unselectbar(R.mipmap.five2, -0xcccccd)
    }

    fun mine() {
        bottomNews.unselectbar(R.mipmap.one2, -0xcccccd)
        bottomPeople.unselectbar(R.mipmap.two2, -0xcccccd)
        bottomVideo.unselectbar(R.mipmap.three2, -0xcccccd)
        bottomLive.unselectbar(R.mipmap.four2, -0xcccccd)
        bottomMine.selectbar(R.mipmap.five, Color.RED)
    }


}
